package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.*;
import com.itheima.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    /**
     * 查询订单
     *
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/page")
    public R selectAll(Integer page, Integer pageSize, String number, String beginTime, String endTime) {

        Page<Orders> page1 = new Page<>(page, pageSize);

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (number != null) {
            wrapper.like("number", number);
        }
        if (beginTime != null && endTime != null) {
            wrapper.ge("checkout_time", beginTime);
            wrapper.le("order_time", endTime);
        }
        wrapper.orderByDesc("order_time");
        ordersService.page(page1, wrapper);

        return R.success(page1);
    }

    /**
     * 订单查询
     * @param page
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("/userPage")
    public R userPage(Integer page, Integer pageSize, HttpSession session) {

        //获取当前登录用户的id
        Long userId = (Long) session.getAttribute("userId");
        //分页查询
        Page<Orders> p = new Page<>(page, pageSize);

        QueryWrapper<Orders> wrapper = new QueryWrapper();


        wrapper.eq("user_id", userId);
        ordersService.page(p, wrapper);

        //创建集合接收每个OrdersDto
        ArrayList<OrdersDto> orders = new ArrayList<>();
        //订单里的所有数据
        List<Orders> records = p.getRecords();
        if (records != null) {
            //遍历
            for (Orders record : records) {
                QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("order_id", record.getId());
                //按时间排序
                wrapper.orderByDesc("order_time");
                //根据订单id查询订单明细的所有数据
                List<OrderDetail> list = orderDetailService.list(queryWrapper);
                //共多少件商品

                OrdersDto dto = new OrdersDto();
                //拷贝
                BeanUtils.copyProperties(record, dto);
                //封装
                dto.setOrderDetails(list);
                orders.add(dto);
            }
        }
        //创建新的page对象
        Page<OrdersDto> objectPage = new Page<>();
        //将查询出的数量存入
        objectPage.setTotal(p.getTotal());
        //数据存入
        objectPage.setRecords(orders);

        return R.success(objectPage);

    }

    /**
     * 添加订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {


        ordersService.add(orders);

        return R.success(null);

    }
}
